<?php
/**
 +------------------------------------------------------------------------------
 * 广告系统
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2016-3-2
 +------------------------------------------------------------------------------
 */
 
namespace Admin\Controller;

class AdController extends AdminController{
    
    /**
     * @todo 广告列表
     */
    public function index() {
        $name = (string)I('get.name','');
        cookie('__forward__',$_SERVER['REQUEST_URI']);
        $map = array();
        if(!empty($name)){
            $map['ad_name'] = array('like','%'.$name.'%');
            $map['status'] = array('egt',0);
        }
        $list = $this->lists('ad',$map,'sort desc, ad_id desc');
        int_to_string($list,array(
            'ad_position_type'=>C('AD_POSITION_TYPE'),
            'status'=>array('-1'=>'删除','0'=>'禁用','1'=>'启用')
        ));
        $this->assign('_list',$list);
        $this->assign('meta_title','广告管理');
        $this->display();
    }
    
    /**
     * @todo 新增广告
     */
    public function add() {
        if(IS_POST){
            $ad_position_id = I('post.ad_position_id','');
            if(empty($ad_position_id)){
                $this->error('请选择广告位置');
            }
            $ad = D('ad');
            $ad_id = $ad->update();
            if(false === $ad_id){
                $this->error($ad->getError());
            }else{
                $ad->updateAdPosition($ad_id,$ad_position_id);
                S('ad_list',null);
                action_log('ad_add', 'Ad', $ad_id, UID);
                $this->success('添加成功！', cookie('__forward__'));
            }
        }
        $map['status'] = 1;
        $field = 'ad_position_id,ad_position_name,ad_position_width,ad_position_height,ad_position_type';
        $ad_position_list = M('ad_position')->field($field)->where($map)->select();
        int_to_string($ad_position_list,array(
            'ad_position_type'=>C('AD_POSITION_TYPE')
        ));
        $this->assign('ad_position_list',$ad_position_list);
        $this->assign('meta_title','添加广告');
        $this->display('edit');
    }
    
    /**
     * @todo 编辑广告
     */
    public function edit() {
        $id = (int)I('get.id');
        if($id == 0){
            $this->error('非法操作，id错误');
        }
        $ad = D('ad');
        if(IS_POST){
            $ad_position_id = I('post.ad_position_id','');
            if(empty($ad_position_id)){
                $this->error('请选择广告位置');
            }
            $ad = D('Ad');
            $ad_id = $ad->update();
            if(false === $ad_id){
                $this->error($ad->getError());
            }else{
                $ad->updateAdPosition($ad_id,$ad_position_id);
                S('ad_list',null);
                action_log('ad_edit', 'Ad', $id, UID);
                $this->success('修改成功！', cookie('__forward__'));
            }
        }
        
        $info = $ad->where(array('ad_id'=>$id))->find();
        if(!$info){
            $this->error('广告信息不存在');
        }
        //广告位置
        $map['status'] = 1;
        $field = 'ad_position_id,ad_position_name,ad_position_width,ad_position_height,ad_position_type';
        $ad_position_list = M('ad_position')->field($field)->where($map)->select();
        int_to_string($ad_position_list,array(
            'ad_position_type'=>C('AD_POSITION_TYPE')
        ));
        
        //选中的位置
        //命名风格   
        $ad_position_id = M('ad_position_ad')->where(array('ad_id'=>$id))->select();
        if(!empty($ad_position_id)){
            $ad_position_id = array_column($ad_position_id, 'ad_position_id');
        }
        $this->assign('ad_position_id',$ad_position_id);
        $this->assign('ad_position_list',$ad_position_list);
        $this->assign('info',$info);
        $this->assign('meta_title','编辑广告');
        $this->display('edit');
    }
    
    /**
     * @todo 广告位置列表
     */
    public function adPosition() {
        $name = (string)I('get.name','');
        cookie('__forward__',$_SERVER['REQUEST_URI']);
        $map = array();
        if(!empty($name)){
            $map['ad_position_name'] = array('like','%'.$name.'%');
            $map['status'] = array('egt',0);
        }
        $list = $this->lists('adPosition',$map);
        int_to_string($list,array(
            'ad_position_type'=>C('AD_POSITION_TYPE'),
            'status'=>array('-1'=>'删除','0'=>'禁用','1'=>'启用')
        ));
        $this->assign('_list',$list);
        $this->assign('meta_title','广告位置管理');
        $this->display();
    }
    
    /**
     * @todo 新增广告位置
     */
    public function addAdPosition() {
        if(IS_POST){
            $ad_position = D('adPosition');
            $id = $ad_position->update();
            if(false === $id){
                $this->error($ad_position->getError());
            }else{
                S('ad_position_list',null);
                action_log('ad_position_add', 'AdPosition', $id, UID);
                $this->success('修改成功！', cookie('__forward__'));
            }
        }
        $this->assign('meta_title','添加广告位置');
        $this->display('editAdPosition');
    }
    
    /**
     * @todo 编辑广告位置
     */
    public function editAdPosition() {
        $id = (int)I('get.id');
        if($id == 0){
            $this->error('非法操作，id错误');
        }
        $ad_position = D('adPosition');
        if(IS_POST){
            if(false === $ad_position->update()){
                $this->error($ad_position->getError());
            }else{
                S('ad_position_list',null);
                action_log('ad_position_edit', 'AdPosition', $id, UID);
                $this->success('修改成功！', cookie('__forward__'));
            }
        }
        $info = $ad_position->where(array('ad_position_id'=>$id))->find();
        if(!$info){
            $this->error('位置信息不存在');
        }
        $this->assign('info',$info);
        $this->assign('meta_title','编辑广告位置');
        $this->display('editAdPosition');
    }
    
    
    /**
     * @todo 广告位置状态
     * @author fzh <fanzhaohai@hrbjjhh.com>
     */
    public function changeAdPostion($method = null){
        $id = array_unique((array)I('ids',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if(empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map[D('Admin/AdPosition')->getPk()] = array('in',$id);
        switch (strtolower($method) ){
            case 'forbid':
                action_log('ad_position_forbid', 'AdPosition', $id, UID);
                $this->forbid('AdPosition', $map );
                break;
            case 'resume':
                action_log('ad_position_resume', 'AdPosition', $id, UID);
                $this->resume('AdPosition', $map );
                break;
            case 'delete':
                action_log('ad_position_delete', 'AdPosition', $id, UID);
                $this->delete('AdPosition', $map);
                break;
            default:
                $this->error('参数非法');
        }
    }
    
    
    /**
     * @todo 广告状态
     * @author fzh <fanzhaohai@hrbjjhh.com>
     */
    public function changeAd($method = null){
        $id = array_unique((array)I('ids',0));
    	$id = is_array($id) ? implode(',',$id) : $id;
    	if(empty($id)) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map[D('Admin/Ad')->getPk()] = array('in',$id);
    	switch (strtolower($method) ){
    		case 'forbid':
    		    action_log('ad_forbid', 'Ad', $id, UID);
    			$this->forbid('Ad', $map );
    			break;
    		case 'resume':
    		    action_log('ad_resume', 'Ad', $id, UID);
    			$this->resume('Ad', $map );
    			break;
    		case 'delete':
    		    action_log('ad_delete', 'Ad', $id, UID);
    			$this->delete('Ad', $map);
    			break;
    		default:
    			$this->error('参数非法');
    	}
    }
    
}